package jp.gr.java_conf.ryz.summarize.client.view;

import java.util.List;

import jp.gr.java_conf.ryz.summarize.client.presenter.PostDecoration;
import jp.gr.java_conf.ryz.summarize.client.presenter.PostDecorationPair;
import jp.gr.java_conf.ryz.summarize.client.presenter.PostDecorationUtil;
import jp.gr.java_conf.ryz.summarize.shared.model.Post;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextArea;

public class ExportPanel extends PopupPanel
{
	private static final String DefaultStyleName = "gwt-ExportPanel";
	
	private List<PostDecorationPair> _postPairs;
	private Panel _mainPanel;
	
	
	public ExportPanel(List<PostDecorationPair> postPairs)
	{
		super();
		
		_postPairs = postPairs;
		
		init();
	}
	
	
	private void init()
	{
		addStyleName(DefaultStyleName);
		
		_mainPanel = new FlowPanel();
		
		TextArea postText = new TextArea();
		postText.setSize("400px", "200px");
		postText.setText(getPostText());
		_mainPanel.add(postText);
		
		_mainPanel.add(new HTML("<br/>"));
		
		Button closeButton = new Button("Close");
		closeButton.addClickHandler(new ClickHandler()
		{
			@Override
			public void onClick(ClickEvent event)
			{
				hide();
			}
		});
		_mainPanel.add(closeButton);
		
		setWidget(_mainPanel);
		center();
	}
	
	private String getPostText()
	{
		StringBuilder sb = new StringBuilder();
		
		for (PostDecorationPair each : _postPairs)
		{
			Post post = each.getPost();
			PostDecoration decoration = each.getPostDecoration();
			sb.append(PostDecorationUtil.getTitleHtml(post, decoration) + "\n");
			sb.append(PostDecorationUtil.getBodyHtml(post, decoration) + "<br />\n");
		}
		
		return sb.toString();
	}
}
